﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 19.03.2008
 * Zeit: 14:28
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
 
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;


namespace Gamadus.Designer
{
	public partial class ResourceMgr
	{
		bool SaveProject(string file)
		{		
			XmlTextWriter xml = new XmlTextWriter(TempDir+"game.xml", null);
			xml.WriteStartDocument();
			xml.WriteStartElement("GamadusProject");
			xml.WriteAttributeString("Version", VersionString);
			xml.WriteStartElement("General");
			xml.WriteStartElement("NextID");
			xml.WriteString(nextID.ToString());
			xml.WriteEndElement();
			xml.WriteEndElement();
			
			xml.WriteStartElement("Resources");
			//iterate all the resources
			foreach(Resource res in Resources.Values)
			{
				xml.WriteStartElement("Resource");
				xml.WriteAttributeString("Type", res.Type.ToString());
				xml.WriteAttributeString("Name", res.Name);
				xml.WriteAttributeString("ID", res.ID.ToString());
				xml.WriteStartElement("Settings");
				foreach(KeyValuePair<string, object> pair in res.Settings)
				{
					xml.WriteStartElement("Set");
					xml.WriteAttributeString("Name", pair.Key);
					WriteObject(pair.Value, xml);
					xml.WriteEndElement();
				}
				xml.WriteEndElement();
				
				res.WriteXml(xml);
				xml.WriteEndElement();	
			}
			
			xml.Flush();
			xml.Close();
			
			XmlDocument doc = new XmlDocument();
			doc.Load(TempDir+"game.xml");
			doc.Save(TempDir+"game.xml");
			
			ZipOutputStream zip = new ZipOutputStream(new StreamWriter(file).BaseStream);
			WriteZipEntry(zip,tempDir+"game.xml","game.xml");
			//TODO: write file resources
			zip.Finish();
			zip.Close();
			File.Delete(TempDir+"game.xml");
			
			this.projectFile = file;
			this.projectTitle = file;
			this.DidChanges = false;
			return true;
		
		}
		
		public void WriteObject(object obj, XmlTextWriter xml)
		{
			xml.WriteStartElement("Object");
			xml.WriteAttributeString("Type",obj.GetType().Name);
			if(obj.GetType() == typeof(string))
				xml.WriteString((string) obj);
			else if(obj.GetType() == typeof(int))
				xml.WriteString(((int)obj).ToString());
			else if(obj.GetType() == typeof(bool))
				xml.WriteString(((bool) obj).ToString());
			else if(obj.GetType() == typeof(System.Drawing.Color))
			{
				System.Drawing.Color col = (System.Drawing.Color) obj;
				xml.WriteStartElement("Color");
				xml.WriteAttributeString("R",col.R.ToString());
				xml.WriteAttributeString("G",col.G.ToString());
				xml.WriteAttributeString("B",col.B.ToString());
				xml.WriteAttributeString("A",col.A.ToString());
				xml.WriteEndElement();
			}
			else
				throw new NotImplementedException();
			
			xml.WriteEndElement();
		}
		
		void WriteZipEntry(ZipOutputStream zip, string file, string target)
		{
			FileStream fs = File.OpenRead(file);
			byte[] buffer = new byte[fs.Length];
			fs.Read(buffer, 0, buffer.Length); 
			ZipEntry entry = new ZipEntry(target);
			zip.PutNextEntry(entry);
			zip.Write(buffer, 0, buffer.Length);
			fs.Close();
		}
		
	}
}